home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_3.9 / binarize / binarize.c next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  5.2 KB  |  183 lines

  1. /*
  2.  * binarize.c
  3.  *
  4.  * Practical Algorithms for Image Analysis
  5.  *
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /* BINARIZE:    program binarizes image with respect to user-chosen threshold
  10.  *                    usage: binarize inimg outimg [-t THRESH] [-i] [-a] [-L]
  11.  */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <tiffimage.h>          /* tiff file format info */
  16. #include <images.h>
  17. #include "misc.h"
  18. extern void print_sos_lic ();
  19.  
  20. #define HI ((unsigned char) 255)  /* binarization values */
  21. #define LO ((unsigned char) 0)
  22. #define THRESH_DFLT ((unsigned char) 128)  /* default threshold value */
  23.  
  24. long usage (short);
  25. long input (int, char **, long *, short *, short *);
  26.  
  27. int argc;
  28. char *argv[];
  29. long *thresh;
  30.  
  31. main (argc, argv)
  32.      int argc;
  33.      char *argv[];
  34. {
  35.   Image *imgI, *imgO;           /* I/O image structures */
  36.   unsigned char **imgIn, **imgOut;  /* input/output images */
  37.   long width, height;           /* input image size */
  38.   long thresh;                  /* threshold value */
  39.   short invertFlag;             /* if =0, dark -> ON; if =1, dark -> OFF */
  40.   short aoiFlag;                /* if =1, will prompt user for subimage area to binarize */
  41.   int i1, j1, i2, j2;
  42.   int c;
  43.   int x1, y1, x2, y2;
  44.   char in_buf[IN_BUF_LEN];
  45.  
  46.   if ((input (argc, argv, &thresh, &invertFlag, &aoiFlag)) < 0)
  47.     return (-1);
  48.  
  49.  
  50. /* open input and output images */
  51.   imgI = ImageIn (argv[1]);
  52.   imgIn = imgI->img;
  53.   height = ImageGetHeight (imgI);
  54.   width = ImageGetWidth (imgI);
  55.  
  56.   printf ("image size is %dx%d; threshold = %d\n", width, height, thresh);
  57.   x1 = y1 = 0;
  58.   x2 = width;
  59.   y2 = height;
  60.   if (aoiFlag != 0) {
  61.     printf ("Specify Area of interest? (y/n) ");
  62.     c = readlin (in_buf);
  63.     if (c == 'y') {
  64.       while (1) {
  65.         printf ("Input upper left lower right coordinates: x0 y0 x1 y1\n");
  66.         if (scanf ("%d %d %d %d", &x1, &y1, &x2, &y2) != 4)
  67.           printf ("Invalid input!  Try again\n");
  68.         else
  69.           break;
  70.       }
  71.     }
  72.   }
  73.   imgO = ImageAlloc (y2 - y1, x2 - x1, 8);
  74.   imgOut = ImageGetPtr (imgO);
  75.  
  76. /* binarize file */
  77.   if (invertFlag == 0) {        /* dark -> ON, light -> OFF */
  78.     for (j1 = y1, j2 = 0; j1 < y2; j1++, j2++)
  79.       for (i1 = x1, i2 = 0; i1 < x2; i1++, i2++)
  80.         if ((long) imgIn[j1][i1] > thresh)
  81.           imgOut[j2][i2] = HI;
  82.         else
  83.           imgOut[j2][i2] = LO;
  84.   }
  85.   else {                        /* dark -> OFF, light -> ON */
  86.     for (j1 = y1, j2 = 0; j1 < y2; j1++, j2++)
  87.       for (i1 = x1, i2 = 0; i1 < x2; i1++, i2++)
  88.         if ((long) imgIn[j1][i1] < thresh)
  89.           imgOut[j2][i2] = HI;
  90.         else
  91.           imgOut[j2][i2] = LO;
  92.   }
  93.  
  94.   ImageOut (argv[2], imgO);
  95.   return (0);
  96. }
  97.  
  98.  
  99.  
  100. /* USAGE:       function gives instructions on usage of program
  101.  *                    usage: usage (flag)
  102.  *              When flag is 1, the long message is given, 0 gives short.
  103.  */
  104.  
  105. long
  106. usage (flag)
  107.      short flag;                /* flag =1 for long message; =0 for short message */
  108. {
  109.  
  110. /* print short usage message or long */
  111.   printf ("USAGE: binarize inimg outimg [-t THRESHOLD] [-i] [-a] [-L]\n");
  112.   if (flag == 0)
  113.     return (-1);
  114.  
  115.   printf ("\nbinarize applies a user-selected threshold to binarize\n");
  116.   printf ("a gray-scale input image, setting pixel intensities\n");
  117.   printf ("BELOW (darker than) the user-chosen threshold to 0\n");
  118.   printf ("and those ABOVE (lighter than) the threshold to 255\n");
  119.   printf ("resulting in a binary output image.\n\n");
  120.   printf ("ARGUMENTS:\n");
  121.   printf ("         inimg: input image filename (TIF)\n");
  122.   printf ("        outimg: output image filename (TIF)\n\n");
  123.   printf ("OPTIONS:\n");
  124.   printf ("  -t THRESHOLD: gray value between 0 and 255 (or other maximum\n");
  125.   printf ("                intensity value); intensities above THRESHOLD are\n");
  126.   printf ("                set to ON, all others to OFF; default = %d.\n", THRESH_DFLT);
  127.   printf ("            -i: INVERT: intensities ABOVE (lighter) threshold set to 0\n");
  128.   printf ("                and those BELOW (darker) threshold set to 255\n");
  129.   printf ("            -a: Area of Interest Flag - will prompt for subimage to binarize\n");
  130.   printf ("            -L: print Software License for this module\n");
  131.   return (-1);
  132. }
  133.  
  134.  
  135. /* INPUT:       function reads input parameters
  136.  *                  usage: input (argc, argv, &thresh)
  137.  */
  138.  
  139. #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
  140.  
  141. long
  142. input (argc, argv, thresh, invertFlag, aoiFlag)
  143.      int argc;
  144.      char *argv[];
  145.      long *thresh;              /* threshold value */
  146.      short *invertFlag;         /* if =0, dark -> ON; if =1, dark -> OFF */
  147.      short *aoiFlag;            /* if =1, prompt for subimage area to binarize */
  148. {
  149.   long n;
  150.  
  151.   *invertFlag = 0;
  152.   *aoiFlag = 0;
  153.  
  154.   if (argc == 1)
  155.     USAGE_EXIT (1);
  156.   if (argc == 2)
  157.     USAGE_EXIT (0);
  158.  
  159.   *thresh = THRESH_DFLT;
  160.  
  161.   for (n = 3; n < argc; n++) {
  162.     if (strcmp (argv[n], "-t") == 0) {
  163.       if (++n == argc || argv[n][0] == '-')
  164.         USAGE_EXIT (0);
  165.       *thresh = atol (argv[n]);
  166.     }
  167.     else if (strcmp (argv[n], "-i") == 0) {
  168.       *invertFlag = 1;
  169.     }
  170.     else if (strcmp (argv[n], "-a") == 0) {
  171.       *aoiFlag = 1;
  172.     }
  173.     else if (strcmp (argv[n], "-L") == 0) {
  174.       print_sos_lic ();
  175.       exit (0);
  176.     }
  177.     else
  178.       USAGE_EXIT (0);
  179.   }
  180.  
  181.   return (0);
  182. }
  183.